$(document).ready(function () {
    // var height = window.innerHeight;
    // console.log(height);
    var allWindows = require('electron').remote.BrowserWindow.getAllWindows();
    var size = allWindows[0].getSize();
    $("#mainInfo").css("max-height", size[1] - 70 + "px");
    var firstPageFlag = true;
    var searchFlag = false;
    var arrayModels = [];
    $.ajax({
        url: "http://localhost:8080/GeoModeling/ModelItemsByUserIdServlet",
        type: "get",
        beforeSend: function (xhr) {
            xhr.withCredentials = true;
        },
        data: { "page": 1, "sortType": "default", "asc": 1 },
        success: function (data) {
            var info = JSON.parse(data);
            console.log(info);
            if (info.count === 0) {
                $("#modelServiceList").empty();
                if (language === "en") {
                    $("#modelServiceList").append("<p>You don't create your own modelItem</p>");
                } else {
                    $("#modelServiceList").append("<p>没有属于自己的模型条目</p>");
                }
                $("#page").empty();
            } else {
                $("#page").initPage(info.count, 1, reGetModelItem);
                createClassInfoDiv(info.modelItems);
            }
        }
    });

    $("#searchInput").keydown(function (e) {
        var ev = window.event || e;
        if (ev.keyCode == 13) {
            $("#searchBtn").trigger("click");
        }
    });

    $("#searchBtn").click(function () {
        var searchName = $("#searchInput").val();
        firstPageFlag = true;
        if (searchName != "") {
            searchFlag = true;
            $.ajax({
                url: "http://localhost:8080/GeoModeling/SearchModelItemsServlet",
                type: "get",
                beforeSend: function (xhr) {
                    xhr.withCredentials = true;
                },
                async: false,
                data: { "searchText": searchName, "asc": "1", "page": 1, "sortType": "default" },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    var info = data;
                    if (info.count === 0) {
                        $("#modelServiceList").empty();
                        if (language === "en") {
                            $("#modelServiceList").append("<p>You don't create your own modelItem</p>");
                        } else {
                            $("#modelServiceList").append("<p>没有属于自己的模型条目</p>");
                        }
                        $("#page").empty();
                    } else {
                        $("#page").initPage(info.count, 1, reGetModelItem);
                        createClassInfoDiv(info.modelItems);
                    }
                }
            });
        } else {
            searchFlag = false;
            $.ajax({
                url: "http://localhost:8080/GeoModeling/ModelItemsByUserIdServlet",
                type: "get",
                beforeSend: function (xhr) {
                    xhr.withCredentials = true;
                },
                async: false,
                data: { "page": 1, "sortType": "nadefaultme", "asc": "1" },
                dataType: "json",
                success: function (data) {
                    var info = data;
                    if (info.count === 0) {
                        $("#modelServiceList").empty();
                        if (language === "en") {
                            $("#modelServiceList").append("<p>You don't create your own modelItem</p>");
                        } else {
                            $("#modelServiceList").append("<p>没有属于自己的模型条目</p>");
                        }
                        $("#page").empty();
                    } else {
                        $("#page").initPage(info.count, 1, reGetModelItem);
                        createClassInfoDiv(info.modelItems);
                    }
                }
            });
        }
    });

    function reGetModelItem(page) {
        if (firstPageFlag) {
            firstPageFlag = false
        } else {
            if (searchFlag) {
                var searchName = $("#searchInput").val();
                $.ajax({
                    url: "http://localhost:8080/GeoModeling/SearchModelItemsServlet",
                    type: "get",
                    beforeSend: function (xhr) {
                        xhr.withCredentials = true;
                    },
                    async: false,
                    data: { "searchText": searchName, "asc": "1", "page": page, "sortType": "default" },
                    dataType: "json",
                    success: function (data) {
                        var result = data;
                        arrayModels = result.modelItems;
                        createClassInfoDiv(result.modelItems);
                    }
                });
            } else {
                $.ajax({
                    url: "http://localhost:8080/GeoModeling/ModelItemsByUserIdServlet",
                    type: "get",
                    beforeSend: function (xhr) {
                        xhr.withCredentials = true;
                    },
                    async: false,
                    data: { "page": page, "sortType": "nadefaultme", "asc": "1" },
                    dataType: "json",
                    success: function (data) {
                        var result = data;
                        arrayModels = result.modelItems;
                        createClassInfoDiv(result.modelItems);
                    }
                });
            }
        }
        //document.documentElement.scrollTop = document.body.scrollTop = 0;
        $("#mainInfo").animate({ scrollTop: 0 }, 500);
    }

    function createClassInfoDiv(infoArray) {
        var mainObj = $("#modelServiceList");
        var language = "en";
        mainObj.empty();
        for (var i = 0; i < infoArray.length; i++) {
            if (language === "en") {
                (function(e){
                    var panel = document.createElement("div");
                    panel.className = "panel panel-default";
                    var panelHeading = document.createElement("div");
                    panelHeading.className = "panel-heading";
                    panelHeading.innerHTML = infoArray[e].model_name;
                    panel.appendChild(panelHeading);
    
                    var pullRight = document.createElement("div");
                    pullRight.className = "pull-right";
                    pullRight.style.marginTop = "-6px";
                    var uid = infoArray[e].model_id;
                    pullRight.setAttribute("uid", uid);
                    panelHeading.appendChild(pullRight);
    
                    var editBtn = document.createElement("button");
                    editBtn.className = "btn btn-primary editBtn";
                    editBtn.innerHTML = "Edit Info";
                    editBtn.style.marginRight = "5px";
                    pullRight.appendChild(editBtn);
                    editBtn.onclick = function(){
                        window.location.href = "editModelItem.html?uid="+uid;
                    }
    
                    /*var renameBtn = document.createElement("button");
                     renameBtn.className = "btn btn-warning";
                     renameBtn.innerHTML = "Rename";
                     renameBtn.style.marginRight = "5px";
                     pullRight.appendChild(renameBtn);*/
    
                    var deleteBtn = document.createElement("button");
                    deleteBtn.className = "btn btn-danger deleteBtn";
                    deleteBtn.innerHTML = "Delete";
                    deleteBtn.onclick = function(){
                        $.ajax({
                            url: "http://localhost:8080/GeoModeling/DeleteModelItemServlet",
                            type: "get",
                            data:{mid:uid},
                            dataType:"json",
                            success:function(result){
                                alert(result);
                                window.location.reload();
                            }
                        })
                    }
                    pullRight.appendChild(deleteBtn);
    
                    var panelBody = document.createElement("div");
                    panelBody.className = "panel-body";
                    panel.appendChild(panelBody);
    
                    var p1 = document.createElement("p");
                    p1.innerHTML = "<b>Create Time:</b>" + infoArray[e].model_createTime;
    
                    var p2 = document.createElement("p");
                    p2.style.wordBreak = "break-all";
                    p2.innerHTML = "<b>Description:</b>" + infoArray[e].model_description;
    
                    panelBody.appendChild(p1);
                    panelBody.appendChild(p2);
                    mainObj.append(panel);
                })(i);               
            }           
        }
    }

    window.onresize = function () {
        var allWindows = require('electron').remote.BrowserWindow.getAllWindows();
        var size = allWindows[0].getSize();
        $("#mainInfo").css("max-height", size[1] - 70 + "px");
    }
});